import {
  globalLayer,
  globalTool
} from '../BQGIS-globalVar'

export function initGraphicLayer() {
  if (!globalLayer.militaryMark) {
    globalLayer.militaryMark = new mars3d.layer.GraphicLayer({
      contextmenuItems: [{text: "开始编辑样式",
      icon: "fa fa-edit",
      show: function (e) {
        const graphic = e.graphic
        if (!graphic || !graphic.hasEdit) {
          return false
        }
        return !graphic.isEditing
      },
      callback: (e) => {
        const graphic = e.graphic
        if (!graphic) {
          return false
        }
        globalTool.eventTarget.fire('changeMilitaryMarkStyle', e)
      }},{
        text: "开始编辑对象",
        icon: "fa fa-edit",
        show: function (e) {
          const graphic = e.graphic
          if (!graphic || !graphic.hasEdit) {
            return false
          }
          return !graphic.isEditing
        },
        callback: (e) => {
          const graphic = e.graphic
          if (!graphic) {
            return false
          }
          if (graphic) {
            globalLayer.militaryMark.startEditing(graphic)
          }
        }
      },
      {
        text: "停止编辑对象",
        icon: "fa fa-edit",
        show: function (e) {
          const graphic = e.graphic
          if (!graphic || !graphic.hasEdit) {
            return false
          }
          return graphic.isEditing
        },
        callback: (e) => {
          const graphic = e.graphic
          if (!graphic) {
            return false
          }
          if (graphic) {
            graphic.stopEditing()
          }
        }
      },
      {
        text: "删除对象",
        icon: "fa fa-trash-o",
        show: (event) => {
          const graphic = event.graphic
          if (!graphic || graphic.isDestroy) {
            return false
          } else {
            return true
          }
        },
        callback: (e) => {
          const graphic = e.graphic
          if (!graphic) {
            return
          }
          graphic.stopEditing()
          globalLayer.militaryMark.removeGraphic(graphic)
        }
      }
      ]
    })
    viewer.addLayer(globalLayer.militaryMark)
  }
  return globalLayer.militaryMark
}

export function drawPolygon(type) {
  initGraphicLayer()
  globalLayer.militaryMark.startDraw({
    type: type,
    styleType: "polygon",
    style: {
      color: "#ff0000",
      opacity: 0.6,
      outline: true,
      outlineWidth: 3,
      outlineColor: "#ffffff",
      clampToGround: true
    }
  })
}

// 绘制(带高度)
export function drawExtrudedPolygon(type) {
  initGraphicLayer()
  globalLayer.militaryMark.startDraw({
    type: type,
    styleType: "polygon",
    style: {
      color: "#ffd500",
      opacity: 0.4,
      diffHeight: 300
    }
  })
}

export function getGeoJson() {
  if(!globalLayer.militaryMark) return
  return globalLayer.militaryMark.toGeoJSON()
}

export function loadGeoJSON(geojson) {
  initGraphicLayer()
  globalLayer.militaryMark.loadGeoJSON(geojson)
}


export function download(content, filename) {
  const element = document.createElement('a');
  element.setAttribute('download', filename);
  element.style.display = 'none';
  // 字符内容转变成blob地址
  var blob = new Blob([JSON.stringify(content)]);
  element.href = URL.createObjectURL(blob);
  // 触发点击
  document.body.appendChild(element);
  element.click();
  // 然后移除
  document.body.removeChild(element);
};

export function getFileByJson(jsonString, fileName) {
  if (!jsonString) return
  const content = typeof jsonString == 'string' ? jsonString : JSON.stringify(jsonString)
  var blob = new Blob([content], {
    type: 'application/json'
  });
  let file = new File([blob], fileName, {
    type: 'application/json'
  })
  return file
}

export function getFileData(e) {
  return new Promise((resolve, reject) => {
    const files = e.target.files;
    if (!files || !files.length) return;
    const file = files[0];
    const reader = new FileReader();
    reader.readAsText(file);
    reader.onload = function () {
      resolve(reader.result)
    };
    reader.onerror = function () {
      reject()
    }
  })
}

export function clearLayer() {
  if(!globalLayer.militaryMark) return
  globalLayer.militaryMark.clear(true)
}